package com.redis.springtest;

import com.redis.springtest.config.RedisConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import redis.clients.jedis.Jedis;

public class AppRedisLua1 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(RedisConfig.class);
        RedisConnectionFactory jedisConnectionFactory = ctx.getBean("jedisConnectionFactory", RedisConnectionFactory.class);
        RedisConnection connection = jedisConnectionFactory.getConnection();
        Jedis jedis = (Jedis) connection.getNativeConnection();

        // 执行简单的脚本
        String helloJava = (String) jedis.eval("return 'hello java'");
        System.out.println(helloJava);

        // 执行带有参数的脚本
        jedis.eval("redis.call('set', KEYS[1], ARGV[1])", 1, "LUA-KEY", "LUA-VALUE");
        String luaKey = jedis.get("LUA-KEY");
        System.out.println(luaKey);

        // 缓存脚本，返回sha1签名
        String sha1 = jedis.scriptLoad("redis.call('set', KEYS[1], ARGV[1])");
        // 通过标识执行脚本
        jedis.evalsha(sha1, 1, "sa-key", "sa-value");
        String s = jedis.get("sa-key");
        // 获取执行脚本后的数据
        System.out.println(s);
        // 关闭连接
        jedis.close();
    }
}
